package com.alphacsp.theedge2012.ui.components.calendar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.view.View;
import com.alphacsp.theedge2012.R;

import java.util.TimeZone;

/**
 * Created by IntelliJ IDEA.
 * User: Gilad
 * Date: 23/10/10
 * Time: 17:54
 */
public class TimeRulerView extends View {

    private static final TimeZone CONFERENCE_TIME_ZONE = TimeZone.getTimeZone("Asia/Jerusalem");

    private int rulerDividerWidth = 70;
    private int rulerScale = 90;
    private int textSize = 20;
    private int textPaddingLeft = 8;
    private int textColor = Color.BLACK;
    private int rulerColor = Color.LTGRAY;
    private int startHour = 8;
    private int endHour = 19;
    private final int numberOfRows;


    public TimeRulerView(Context context) {
        this(context, null);
    }

    public TimeRulerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TimeRulerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        final TypedArray viewAttributes = context.obtainStyledAttributes(attrs, R.styleable.TimeRulerView, defStyle, 0);

        this.rulerDividerWidth = viewAttributes.getDimensionPixelSize(R.styleable.TimeRulerView_rulerDividerWidth, rulerDividerWidth);
        this.rulerScale = viewAttributes.getDimensionPixelSize(R.styleable.TimeRulerView_rulerScale, rulerScale);
        this.textSize = viewAttributes.getDimensionPixelSize(R.styleable.TimeRulerView_textSize, textSize);
        this.textPaddingLeft = viewAttributes.getDimensionPixelSize(R.styleable.TimeRulerView_textPaddingLeft, textPaddingLeft);
        this.textColor = viewAttributes.getColor(R.styleable.TimeRulerView_textColor, textColor);
        this.rulerColor = viewAttributes.getColor(R.styleable.TimeRulerView_rulerColor, rulerColor);
        this.startHour = viewAttributes.getInt(R.styleable.TimeRulerView_startHour, startHour);
        this.endHour = viewAttributes.getInt(R.styleable.TimeRulerView_endHour, endHour);
        this.numberOfRows = endHour - startHour;
        viewAttributes.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        final Paint rulerPaint = new Paint();
        final Paint textPaint = new Paint();

        rulerPaint.setColor(rulerColor);
        rulerPaint.setStrokeWidth(3);
        textPaint.setColor(textColor);
        textPaint.setTextSize(textSize);
        textPaint.setTypeface(Typeface.DEFAULT_BOLD);
        textPaint.setAntiAlias(true);


        for (int i = 0; i < numberOfRows; i++) {
            canvas.drawLine(0, rulerScale * i, getRight(), rulerScale * i, rulerPaint);
            canvas.drawRect(0, rulerScale * i, rulerDividerWidth, rulerScale * (i + 1), rulerPaint);
            final int hour = startHour + i;
            String text;
            if (DateFormat.is24HourFormat(getContext())) {
                text = hour + ":00";
            } else {
                if (hour == 0) {
                    text = "12am";
                } else if (hour <= 11) {
                    text = hour + "am";
                } else if (hour == 12) {
                    text = "12pm";
                } else {
                    text = (hour - 12) + "pm";
                }
            }
            final float labelWidth = textPaint.measureText(text);
            final Paint.FontMetricsInt metrics = textPaint.getFontMetricsInt();
            final int labelHeight = Math.abs(metrics.ascent);
            final int labelOffset = labelHeight + ((rulerScale - labelHeight) / 2);

            canvas.drawText(text, 0, text.length(), rulerDividerWidth - labelWidth - textPaddingLeft, rulerScale * i + labelOffset, textPaint);
        }


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(resolveSize(rulerDividerWidth, widthMeasureSpec), resolveSize(rulerScale * numberOfRows, heightMeasureSpec));
    }

    public int getStartHour() {
        return startHour;
    }

    public int getRulerDividerWidth() {
        return rulerDividerWidth;
    }

    public int getRulerScale() {
        return rulerScale;
    }
}
